package com.yuzhou.collegeapp.Activity;


import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.yuzhou.collegeapp.Fragment.College;
import com.yuzhou.collegeapp.Fragment.Friend;
import com.yuzhou.collegeapp.Fragment.Life;
import com.yuzhou.collegeapp.Fragment.Mine;
import com.yuzhou.collegeapp.Fragment.News;
import com.yuzhou.collegeapp.R;

public class MainActivity extends AppCompatActivity {
    College college=null;
    Life life=null;
    Friend friend=null;
    News news=null;
    Mine mine=null;
    RadioGroup group;
    RadioButton rdItem;
    FragmentManager fm;
    FragmentTransaction ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fm=getFragmentManager();
        RGonClick();
    }

    public void RGonClick(){
        group=(RadioGroup)findViewById(R.id.group);
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                ft=fm.beginTransaction();
                hideAllFragment(ft);
                switch (checkedId){
                    case R.id.college:
                        if (null==college){
                            college=new College();
                            ft.add(R.id.frame,college);
                        }else {
                            ft.show(college);
                        }
                        break;
                    case R.id.life:
                        if (null==life){
                            life=new Life();
                            ft.add(R.id.frame,life);
                        }else {
                            ft.show(life);
                        }
                        break;
                    case R.id.friend:
                        if (null==friend){
                            friend=new Friend();
                            ft.add(R.id.frame,friend);
                        }else {
                            ft.show(friend);
                        }
                        break;
                    case R.id.news:
                        if (null==news){
                            news=new News();
                            ft.add(R.id.frame,news);
                        }else {
                            ft.show(news);
                        }
                        break;
                    case R.id.mine:
                        if (null==mine){
                            mine=new Mine();
                            ft.add(R.id.frame,mine);
                        }else {
                            ft.show(mine);
                        }
                        break;
                }
                ft.commit();
            }
        });

      rdItem=(RadioButton)findViewById(R.id.college);
      rdItem.setChecked(true);

    }



    public void hideAllFragment(FragmentTransaction ft){
        if (null!=college){
            ft.hide(college);
        }
        if (null!=life){
            ft.hide(life);
        }
        if (null!=friend){
            ft.hide(friend);
        }
        if (null!=news){
            ft.hide(news);
        }
        if (null!=mine){
            ft.hide(mine);
        }
    }





}
